decimal

type decimal

A real number data type with high precision.

Not a complete equivalent of floating-point types, as there are a fixed maximum number of digits before the decimal point (131072, or 2^17 digits) and after the decimal point (20 digits).

Examples:

  • 123.456

  • .789

  • 7e+33

  • decimal('123456789.98765')

Since

0.9.1

Constructors

Link copied to clipboard
pure constructor(value: text)

Construct a decimal from a text representation.

pure constructor(value: integer)

Construct a decimal from an integer.

pure constructor(value: big_integer)

Construct a decimal from a big_integer.

Properties

Link copied to clipboard
val INT_DIGITS: integer = 131072

The maximum number of digits before the decimal point (131072).

Link copied to clipboard
val MAX_VALUE: decimal = 1e+131072

The largest value that a decimal can store (1E+131072 - 1).

Link copied to clipboard
val MIN_VALUE: decimal = 1.0E-20

The smallest nonzero absolute value that a decimal can store (0.00000000000000000001).

Link copied to clipboard
val PRECISION: integer = 131092

The maximum number of digits a decimal can have (131072 before the decimal point and 20 after, i.e. 131092).

Link copied to clipboard
val SCALE: integer = 20

The maximum number of decimal digits after the decimal point (20).

Functions

Link copied to clipboard
pure function abs(): decimal

Returns the absolute value of this decimal; i.e. the decimal itself if it's positive or its negation if it's negative.

Link copied to clipboard
pure function ceil(): decimal

Round this decimal away from zero, to the next whole number.

Examples:

  • (-0.4).ceil() returns -1

  • (-1.99999).ceil() returns -2

  • (1.99999).ceil() returns 2

  • (1.00000000000000000001).ceil() returns 2

Link copied to clipboard
pure function floor(): decimal

Round this decimal towards zero, to the next whole number.

Examples:

  • (-0.4).floor() returns 0

  • (-1.6).floor() returns -1

  • (1.99999999999999999999).floor() returns 1

Link copied to clipboard
pure static function from_text(value: text): decimal

Parse a signed base-10 text representation of a real number.

If the encoded value has more decimal places than are supported, it is rounded to the nearest supported value.

Link copied to clipboard
pure function max(value: decimal): decimal

Returns the greater of this and another decimal value; i.e. value if value is greater than this, or this decimal otherwise.

Link copied to clipboard
pure function min(value: decimal): decimal

Returns the lesser of this and another decimal value; i.e. value if value is less than this, or this decimal otherwise.

Link copied to clipboard
pure function round(): decimal

Round this decimal to the nearest whole number.

Examples:

  • (-0.4).round() returns 0

  • (0.4).round() returns 0

  • (1.49999999999999999999).round() returns 1

  • (1.5).round() returns 2

pure function round(digits: integer): decimal

Round this decimal to a specific number of decimal places.

decimal.round(0) is equivalent to decimal.round(), i.e. rounding will be to the nearest whole number. Positive arguments round to an increasing number of decimal places, e.g. 1 rounds to the neartest tenth, 2 to the nearest hundredth, 3 to the nearest thousandth. Negative arguments round in the opposite way, i.e. -1 rounds to the nearest ten, -2 to the nearest hundred, -3 to the nearest thousand.

Examples:

  • (123.456).round(0) returns 123

  • (123.456).round(-1) returns 120

  • (123.456).round(1) returns 123.4

  • (123.456).round(-2) returns 100

  • (123.456).round(2) returns 123.45

  • (123.456).round(-3) returns 0

  • (123.456).round(3) returns 123.456

Link copied to clipboard
pure function sign(): integer

Returns the sign of this decimal: -1 if negative, 0 if zero, and 1 if positive.

It holds that for all x, x == x.sign() * x.abs().

Link copied to clipboard
(alias) pure function signum(): integer

Returns the sign of this decimal: -1 if negative, 0 if zero, and 1 if positive.

It holds that for all x, x == x.sign() * x.abs().

Alias
Link copied to clipboard
pure function to_big_integer(): big_integer

Convert this decimal to a big_integer, truncating the fractional part.

Numerically equivalent to decimal.floor(), but with a type conversion.

Link copied to clipboard
pure function to_integer(): integer

Convert this decimal to an integer, truncating the fractional part.

Numerically equivalent to decimal.floor(), but with a type conversion.

Link copied to clipboard
pure function to_text(): text

Convert this decimal to a base 10 text representation.

pure function to_text(scientific: boolean): text

Convert this decimal to a base 10 text representation, optionally using scientific notation (also called standard form), e.g. 6.0221E+23.

The general output format when scientific notation is used is mEsn, where m represents a non-zero coefficient, E is the literal character, s represents the sign (either + or -), and n represents the exponent.

Note that the maximum number of decimal places for the coefficient is 20, and therefore any precision beyond this in the original decimal value is lost.